package GUI.AddBooking;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import comparators.DoubleComparator;
import comparators.IntegerComparator;

import GUI.Controller.Controller;
import GUI.TabPanels.BookingPanel;

@SuppressWarnings("serial")
public class ViewBookingPanel extends JPanel{
	private BookingPanel booking_panel;
	private JTable bookingTable;
	private JButton add_booking_button;
	private JScrollPane scrollPane;
	private JComboBox search_choices;

	private JTextField searchField;
	private JButton search_button;

	private String[] columnNames =
		{
			"Booking #", "First Name", "Last Name", "Email",
			"Room #", "Date In", "Date Out", "Cost", "Status" 
		};

	public ViewBookingPanel(){
		super(new BorderLayout());
		booking_panel = BookingPanel.getInstance();
		SetUpTopPane();
		SetUpTable();		
	}

	public void SetUpTopPane(){
		JPanel topPane = new JPanel();
		topPane.setLayout(new FlowLayout());

		add_booking_button = new JButton("Add New Booking");
		add_booking_button.addActionListener(new AddRoomButton());
		searchField = new JTextField(25);
		search_button = new JButton("Search");
		search_choices = new JComboBox(new Object[] { "Search by:", "Booking #", "Last Name", "Room #" });
		topPane.add(add_booking_button);
		topPane.add(search_choices);		
		topPane.add(searchField);
		search_button.addActionListener(new SearchButton());
		topPane.add(search_button);


		this.add(topPane, BorderLayout.NORTH);
	}

	public void SetUpTable(){
		Object[][] tableData = Controller.GetBookings(null, null);
		CreateTable(tableData);
	}

	void CreateTable(Object[][] tableData){
		bookingTable = new JTable(tableData, columnNames){
			@Override 
			public boolean isCellEditable(int row, int column) { 
				return false; 
			} 
		};
		bookingTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableModel model = new DefaultTableModel(tableData, columnNames);

		TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);

		sorter.setComparator(0, new IntegerComparator());
		sorter.setComparator(4, new IntegerComparator());
		sorter.setComparator(7, new DoubleComparator());

		bookingTable.setRowSorter(sorter);

		bookingTable.setPreferredScrollableViewportSize(new Dimension(820, 470));
		bookingTable.setFillsViewportHeight(true);
		bookingTable.updateUI();

		if(scrollPane != null)
			remove(scrollPane);

		scrollPane = new JScrollPane(bookingTable);
		this.add(scrollPane, BorderLayout.SOUTH);
		this.setVisible(false);
		this.setVisible(true);
	}

	public void Update(){
		SetUpTable();

		bookingTable.updateUI();

		this.setVisible(false);
		this.setVisible(true);
	}

	private class AddRoomButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			booking_panel.AddBookings();
		}
	}

	private class SearchButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			DefaultTableModel model = new DefaultTableModel(Controller.GetBookings(null, null), columnNames);

			bookingTable.setModel(model);

			RowFilter<Object, Object> filter = new RowFilter<Object, Object>()
					{
				public boolean include(Entry<?, ?> entry)
				{
					int index = search_choices.getSelectedIndex();
					if(index == 0){
						index = 1;
					}else if(index == 1){
						index = 0;
					}else if(index == 2){
						index = 2;
					}else if(index == 3){
						index = 4;
					}
					return ((entry.getStringValue(index).compareTo(searchField.getText()))== 0) || (searchField.getText().length() < 1) || index == 1;
				}
					};

					TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);
					sorter.setRowFilter(filter);

					if (searchField.getText().length() < 1)
						sorter.setRowFilter(null);
					bookingTable.setRowSorter(sorter);
		}

	}

	public void UpdateTable(){
		new SearchButton().actionPerformed(null);
	}		

}
